<template>
	<view class="container">
		<!-- <view class="left-bottom-sign"></view>
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<view class="right-top-sign"></view> -->
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<!-- <view class="left-top-sign">LOGIN</view> -->
			<view class="welcome">
				Let's get started
			</view>
			<view class="input-content">
				<view class="input-wrapper">
					<view class="input-item">
						<text class="tit">Full Name</text>
						<input 
							value="" 
							placeholder="Type your Full Name"
							maxlength="11"
							data-key="fullName"
							@input="inputChange"
						/>
					</view>
					<image class="input-check" v-if="showCheckImg_fullName" src="/static/images/Check-input.png"></image>
				</view>
				<view class="input-wrapper">
					<view class="input-item">
						<text class="tit">Password</text>
						<input 
							type="mobile" 
							value="" 
							placeholder="Type your Password"
							placeholder-class="input-empty"
							maxlength="20"
							password 
							data-key="password"
							@input="inputChange"
							@confirm="toLogin"
						/>
					</view>
					<image class="input-check" v-if="showCheckImg_password" src="/static/images/Check-input.png"></image>
				</view>
			</view>
			<view class="contact-section">
				What is your Contact?
			</view>
			<view class="input-content">
				<view class="input-wrapper mobileArea">
					<view class="picker-container mg-r10" > <!-- @click="countrySelect" -->
						<!-- <picker ref="mobileArea" @change="bindPickerChange" :value="mobileAreaIndex" :range="mobileAreaList" range-key="text"> -->
							<!-- <view class="uni-input">{{mobileAreaList[mobileAreaIndex]}}</view> -->
							<view class="uni-input mg-r10">
								<image class="mobile-area-img" mode='aspectFit' :src="mobileAreaList[mobileAreaIndex[0]] ? mobileAreaList[mobileAreaIndex[0]].src : ''"></image>
							</view>
						<!-- </picker> -->
						<!-- <uni-icons color="#000000" type="arrowdown" size="18" @click="arrowDownClick" /> -->
					</view>
					<view class="input-item">
						<input 
							type="number" 
							value="" 
							:placeholder="mobilePlaceholder"
							placeholder-class="input-empty"
							maxlength="20"
							data-key="mobile"
							@input="inputChange"
						/>
					</view>
				</view>
				<view class="input-wrapper">
					<image class="input-label-img" src="/static/images/mail.png"></image>
					<view class="input-item">
						<input 
							type="email" 
							value="" 
							placeholder="Type your email"
							placeholder-class="input-empty"
							maxlength="20"
							data-key="email"
							@input="inputChange"
						/>
					</view>
				</view>
			</view>
			<button class="confirm-btn" @click="toNext" :loading="logining">Next</button>
			<mpvue-picker
				themeColor="#007AFF"
				ref="mpvuePicker"
				:isCountrySelect="true"
				:pickerValueDefault="mobileAreaIndex"
				@onConfirm="onConfirm"
				@onCancel="onCancel"
				:pickerValueArray="mobileAreaList"
			></mpvue-picker>
		</view>
	</view>
</template>

<script>
	import {  
        mapMutations  
    } from 'vuex';
	import uniIcons from "@/components/uni-icons/uni-icons.vue";
	import mpvuePicker from '@/components/mpvue-picker/mpvuePicker.vue';
	export default{
		components: {
			uniIcons,
			mpvuePicker
		},
		data(){
			return {
				fullName: '',
				password: '',
				mobile: '',
				email: '',
				logining: false,
				showCheckImg_fullName: false,
				showCheckImg_password: false,
				mobileAreaIndex: [0],
				mobileAreaList: ['+86'],
			}
		},
		onLoad(){
			this.loadData();
		},
		computed: {
			mobilePlaceholder() {
				let areaText = '+' + this.mobileAreaList[this.mobileAreaIndex[0]].id || ''
				return areaText + ' Mobile number'
			}
		},
		methods: {
			...mapMutations(['login']),
			async loadData() {
				this.mobileAreaList = await this.$api.json('mobileAreaList')
				console.log(this.mobileAreaList)
			},
			// 绑定数据
			inputChange(e){
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
				if (e.detail.value.length > 0) {
				    this['showCheckImg_' + key] = true;
				} else {
				    this['showCheckImg_' + key] = false;
				}
			},
			bindPickerChange(e) {
				this.mobileAreaIndex = e.target.value
			},
			arrowDownClick() {
				this.$refs.mpvuePicker.$el.click();
			},
			navBack(){
				uni.navigateBack();
			},
			// 选择国家
			countrySelect() {
				this.$refs.mpvuePicker.show()
			},
			onConfirm(val) {
				this.mobileAreaIndex = val.index
			},
			onCancel() {
				
			},
			// 合法性校验
			checkInput() {
				let res = {};
				if (!this.fullName) {
					res.status = false;
					res.msg = 'Please enter the Full Name';
				} else if (!this.password) {
					res.status = false;
					res.msg = 'Please enter the password';
				} else if (!this.mobile) {
					res.status = false;
					res.msg = 'Please enter the mobile';
				} else if (!this.email) {
					res.status = false;
					res.msg = 'Please enter the email';
				} else {
					res.status = true;
				}
				return res;
			},
			// 下一步
			async toNext(){
				if(!this.checkInput().status) {
					this.$common.errorToShow(this.checkInput().msg);
					return;
				}
				this.logining = true;
				let data = {
					user_login: this.fullName,
					mobile: this.mobile,
					user_email: this.email,
				}
				let _this = this;
				// 校验数据
				this.$api.registerCheck(data, res => {
					if(res.code === 1 && res.msg == 'success') {
						// 密码
						data.password = _this.password;
						// 国家代码
						data.national_code = _this.mobileAreaList[_this.mobileAreaIndex[0]].id
						_this.$api.register(data, res1 => {
							if(res1.code === 1) {
								const sendData = {
									username: _this.fullName,
									password: _this.password
								};
								
								_this.$api.login(sendData, res2 => {
									if(res2.code === 1) {
										_this.$db.set('userToken', res2.data.token);
										_this.login(res2.data);
										_this.$common.navigateTo('register2?name=' + res2.data.user.user_login + '&mobile=' + res2.data.user.mobile)
									} else {
										_this.$common.errorToShow(res2.msg)
										_this.logining = false
									}
								})
							} else {
								_this.logining = false
								_this.$common.errorToShow(res1.msg)
							}
						})
					} else {
						_this.$common.errorToShow(res.msg)
						_this.logining = false
					}
				})
			}
		},

	}
</script>

<style lang='scss'>
	page{
		background: #fff;
	}
	.container{
		position:relative;
		overflow: hidden;
		background: #fff;
	}
	.wrapper{
		margin-top: 115rpx;
		position:relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}
	.back-btn{
		position:absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}
	.left-top-sign{
		font-size: 120upx;
		color: $page-color-base;
		position:relative;
		left: -16upx;
	}
	.right-top-sign{
		position:absolute;
		top: 80upx;
		right: -30upx;
		z-index: 95;
		&:before, &:after{
			display:block;
			content:"";
			width: 400upx;
			height: 80upx;
			background: #b4f3e2;
		}
		&:before{
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}
		&:after{
			position: absolute;
			right: -198upx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}
	.left-bottom-sign{
		position:absolute;
		left: -270upx;
		bottom: -320upx;
		border: 100upx solid #d0d1fd;
		border-radius: 50%;
		padding: 180upx;
	}
	.welcome{
		position:relative;
		left: 50upx;
		top: -40upx;
		font-weight: bold;
		font-size: $font-lg + 2upx;
		color: #1C1C1C;
	}
	.input-content{
		padding: 0 30rpx;
		.input-wrapper {
			display:flex;
			flex-direction: row;
			flex-wrap: nowrap;
			align-items: center;
			padding: 0 0 0 30upx;
			height: 110upx;
			border-radius: 4px;
			margin-bottom: 50upx;
			border: 2rpx solid #D8D8D8;
			.input-check {
				height: 110upx;
				width: 110upx;
				padding: 22rpx;
			}
			.input-label-img {
				height: 50rpx;
				width: 50rpx;
				margin-right: 30rpx;
			}
			.input-item{
				display:flex;
				flex-direction: column;
				align-items:flex-start;
				justify-content: center;
				flex: 1;
				&:last-child{
					margin-bottom: 0;
				}
				.tit{
					height: 50upx;
					line-height: 56upx;
					font-size: $font-sm+2upx;
					color: #8A8A8A;
				}
				input{
					height: 60upx;
					font-size: $font-base + 2upx;
					color: $font-color-dark;
					width: 100%;
				}
			}
		}
        .mobileArea {
            padding-left: 28rpx;
        };
	}

	.confirm-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 60upx;
		z-index: 95;
		width: 700upx;
		height: 80upx;
		line-height: 80upx;
		border-radius: 10rpx;
		background: #4666D5;
		color: #fff;
		font-family: Poppins;
		font-size: $font-base;
		font-weight: bold;
		box-shadow: 0 20rpx 60rpx rgba(64, 191, 255, 0.24);
		/* &:after{
			border-radius: 100px;
		} */
	}
	.contact-section{
		font-size: $font-base;
		color: #1C1C1C;
		text-align: left;
		margin-top: 40upx;
		padding: 0 30rpx 40rpx;
	}
	.register-section{
		margin-top: 40upx;
		padding: 0 30rpx;
		width: 100%;
		text-align: right;
		text {
			font-size: $font-sm+2upx;
			color: #EB658D;
		}
	}
	.picker-container {
		display: flex;
		flex-direction: row;
		align-items: baseline;
		picker {
			margin-right: 10rpx;
		}
	}
	.mg-r10 {
		margin-right: 10rpx;
	}
	.mobile-area-img {
        vertical-align: middle;
		width: 52rpx;
		height: 52rpx;
	}
</style>
